﻿//翻页-------------------------------------------------------------------------------
function turnPage(pageIndex) {
    var strWhereVal = $("#strWhere").val()
    if ($("#strWhere") == undefined || $("#strWhere") == null ||
        $("#strWhere").val() == undefined || $("#strWhere").val() == null)
        getDataDrawTable("", pageIndex, UrlAction);
    else
        getDataDrawTable(strWhereVal, pageIndex, UrlAction);
}
function homePage() {
    turnPage(1);
}
function previousPage() {
    turnPage(parseInt($("#PageIndex").val()) - 1);
}
function nextPage() {
    turnPage(parseInt($("#PageIndex").val()) + 1);
}
function endPage() {
    turnPage(parseInt($("#TotalPages").val()));
}

function getDataDrawTable(strWhere, pageIndex, actionUrl) {
    $.ajax({
        url: actionUrl,
        data: { strWhere: strWhere, pageIndex: pageIndex },
        type: "POST",
        dataType: 'text',
        success: function (data) {
            var pageTable = eval('(' + data + ')');
            drawTable(pageTable.DataTable);
            MakePagination(pageTable);
        },
        error: function (xhr, textStatus, error) {
            alert(error);
        }
    });
}

function MakePagination(pageTable) {
    var TotalCount = pageTable.TotalCount;//总记录数
    var PageSize = pageTable.PageSize;//每页记录数

    var HasPreviousPage = pageTable.HasPreviousPage;//是否有上一页
    var HasNextPage = pageTable.HasNextPage;//是否有下一页
    var PageIndex = pageTable.PageIndex;//当前页
    var TotalPages = pageTable.TotalPages;//总页数

    $("#PageIndex").val(PageIndex);
    $("#TotalPages").val(TotalPages);

    var strHomePagePreviousPage = "<a href='javascript:void(0)' onclick='homePage()'>首页</a>"
                                + "<a href='javascript:void(0)' onclick='previousPage()'>"
                                    + "<img src='../css/image/tubiao_47.png' class='list-IMG' />"
                                + "</a>";
    var strNextPageEndPage = "<a href='javascript:void(0)' onclick='nextPage()'>"
                                    + "<img src='../css/image/tubiao_50.png' class='list-IMG' />"
                                + "</a>"
                                + "<a href='javascript:void(0)' onclick='endPage()'>尾页</a>";
    strHomePagePreviousPage = HasPreviousPage ? strHomePagePreviousPage : "";
    strNextPageEndPage = HasNextPage ? strNextPageEndPage : "";

    var strPagination = "<div class='list-YeMa' id='divYeMa'>"
                            + "<span class='JILU'>共 " + TotalCount + " 条记录，每页 " + PageSize + " 条</span>"
                            + "<div class='TiaoZhuan'>"
                                + strHomePagePreviousPage
                                + "<span class='currentPageSpan'>" + PageIndex + "/" + TotalPages + "页</span>"
                                + strNextPageEndPage
                            + "</div>"
                        + "</div>";
    $("#divYeMa").remove();
    $("#divTabel").append($(strPagination));
}
//utility-------------------------------------------------------------------------------
function removeAll(tableId) {//清除table已有的内容
    $("#" + tableId + " tr:not(:first)").remove();
}

function format(value) {
    return value == null ? "" : value;
}
